import React from 'react';
import {
  Form,
  Modal,
  Card,
  Input,
  InputNumber,
  Switch,
  Button,
  Icon,
  Row,
  Col
} from 'antd';
import { LwjUpload, LwjDownload } from 'components';
import {
  checkAmount
} from 'utils/enumConfig'


const FormItem = Form.Item
const formItemLayout = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 14
  }
};
const quoteForm = ({
  dispatch,
  onOk,
  isUpdate,
  currentContractDetail,
  isFromThirdPart,
  form: {
    getFieldDecorator,
    getFieldsValue,
    setFieldsValue,
    validateFieldsAndScroll
  },
  ...updateModalProps
}) => {
  // 处理数据
  if (_.isEmpty(currentContractDetail.orderQuoteSummary)) {
    currentContractDetail.orderQuoteSummary = {};
  }

  const handleSubmit = () => {
    validateFieldsAndScroll((err, values) => {
      if (!err) {
        // 处理日期数据
        if (checkAmount(values.preOrderAmount - values.decorationRebateAmount - values.cabinetRebateAmount - values.eventsAmount) !== checkAmount(values.orderAmount)) {
          Modal.info({ title: '提示', content: `金额错误，订单金额 = 原订单金额 - 装修返点金额 - 样柜返点金额 - 活动优惠金额` });
          return;
        }
        values.version = currentContractDetail.version;
        delete values.quoteDeniedReason
        delete values.backupDesignAttachment
        values.deductionType = values.deductionType ? "COUPONS" : "BALANCE"
        onOk(values);
      }
    });
  };

  const modalOpts = {
    ...updateModalProps,
    onOk: handleSubmit
  }

  const xlsProps = {
    maxfile: 1,
    target: '/services/upload/order',
    accept: '.xlsx,.xls,.zip,.rar',
    uploadIcon: 'upload',
    fileType: '',
    onChange(value) { },
    showUploadList: true
  }
  const xlsPropsDesign = {
    maxfile: 1,
    target: '/services/upload/order',
    accept: 'image/*,.pdf,.dwg,.dwf,.zip,.rar,.xlsx,.xls',
    uploadIcon: 'upload',
    fileType: '',
    onChange(value) { },
    showUploadList: true
  }

  const getLastMoney = () => {
    let fields = getFieldsValue();
    if (checkAmount(fields.preOrderAmount) < 0 || checkAmount(fields.decorationRebateAmount) < 0 || checkAmount(fields.cabinetRebateAmount) < 0 || checkAmount(fields.eventsAmount) < 0) {
      Modal.error({ title: '输入金额不能小于0，请重新输入' })
      fields.orderAmount = undefined;
      setFieldsValue(fields)
    }

    if (fields.preOrderAmount >= 0 && fields.decorationRebateAmount >= 0 && fields.cabinetRebateAmount >= 0 && fields.eventsAmount >= 0) {
      const orderAmount = checkAmount(fields.preOrderAmount - fields.decorationRebateAmount - fields.cabinetRebateAmount - fields.eventsAmount);
      if (orderAmount > 0) {
        fields.orderAmount = orderAmount;
        setFieldsValue(fields)
      } else {
        Modal.error({ title: '订单优惠后金额不能小于0，请仔细核对！' })
        fields.orderAmount = undefined;
        setFieldsValue(fields)
      }
    }
  }
  return (
    <Modal {...modalOpts}>
      {(currentContractDetail.status === "PENDING_QUOTE" || currentContractDetail.status === "DENIED_QUOTED") && <Card bordered={false}>
        <Form onSubmit={handleSubmit} hideRequiredMark>
          <Row>
            <Col span={12}>
              {currentContractDetail.status === "DENIED_QUOTED" && <FormItem {...formItemLayout} label="报价不通过原因">
                {getFieldDecorator('quoteDeniedReason', { initialValue: currentContractDetail.orderQuoteSummary.quoteDeniedReason })(<Input disabled />)}
              </FormItem>}
              <FormItem {...formItemLayout} label="原合同金额">
                {getFieldDecorator('preOrderAmount', {
                  rules: [
                    {
                      required: true,
                      message: '请输入原合同金额'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.preOrderAmount
                })(<InputNumber
                  onBlur={getLastMoney}
                  disabled={currentContractDetail.isFromThirdPart}
                  min={0}
                  style={{
                    width: "100%"
                  }}
                  placeholder="请输入原订单金额" />)}
              </FormItem>
              <FormItem {...formItemLayout} label="装修返点金额">
                {getFieldDecorator('decorationRebateAmount', {
                  rules: [
                    {
                      required: true,
                      message: '请输入装修返点金额'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.decorationRebateAmount
                })(<InputNumber
                  onBlur={getLastMoney}
                  style={{
                    width: "100%"
                  }}
                  placeholder="请输入装修返点金额" />)}
              </FormItem>
              <FormItem {...formItemLayout} label="样柜返点金额">
                {getFieldDecorator('cabinetRebateAmount', {
                  rules: [
                    {
                      required: true,
                      message: '请输入样柜返点金额'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.cabinetRebateAmount
                })(<InputNumber
                  onBlur={getLastMoney}
                  style={{
                    width: "100%"
                  }}
                  placeholder="请输入样柜返点金额" />)}
              </FormItem>
              <FormItem {...formItemLayout} label="活动优惠金额">
                {getFieldDecorator('eventsAmount', {
                  rules: [
                    {
                      required: true,
                      message: '请输入活动优惠金额'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.eventsAmount
                })(<InputNumber
                  onBlur={getLastMoney}
                  style={{
                    width: "100%"
                  }}
                  placeholder="请输入活动优惠金额" />)}
              </FormItem>
              <FormItem {...formItemLayout} label="合同金额">
                {getFieldDecorator('orderAmount', {
                  rules: [
                    {
                      required: true,
                      message: '请输入合同金额'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.orderAmount
                })(<InputNumber
                  disabled
                  style={{
                    width: "100%"
                  }}
                  placeholder="请输入合同金额" />)}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem {...formItemLayout} label="报价备注">
                {getFieldDecorator('memo', {
                  rules: [
                    {
                      required: true,
                      message: '请输入报价备注',
                      whitespace: true
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.memo
                })(<Input placeholder="请输入报价备注" />)}
              </FormItem>
              <FormItem {...formItemLayout} label="报价方案">
                {getFieldDecorator('quoteAttachment', {
                  rules: [
                    {
                      required: true,
                      message: '请上传报价方案'
                    }
                  ],
                  initialValue: currentContractDetail.orderQuoteSummary.quoteAttachment
                })(<LwjUpload
                  {...xlsProps}
                  fileLists={currentContractDetail.orderQuoteSummary.quoteAttachment} />)}
              </FormItem>
              {!isFromThirdPart && <FormItem
                {...formItemLayout}
                label="使用优惠券"
              >
                {getFieldDecorator('deductionType', { valuePropName: 'checked', initialValue: false })(
                  <Switch />
                )}
              </FormItem>}

              {!_.isEmpty(currentContractDetail.backupDesignAttachment) && currentContractDetail.productType !== 'END' && <FormItem {...formItemLayout} label="原设计方案">
                {getFieldDecorator('backupDesignAttachment', {
                  initialValue: currentContractDetail.backupDesignAttachment || ''
                })(<LwjDownload
                  title="点击下载"
                  targetUrl={currentContractDetail.backupDesignAttachment || ''} />)}
              </FormItem>}
              {currentContractDetail.productType !== 'END' && <FormItem {...formItemLayout} label="下推设计方案">
                {getFieldDecorator('designAttachment', {
                  rules: [
                    {
                      required: true,
                      message: '请上传设计方案'
                    }
                  ],
                  initialValue: currentContractDetail.designAttachment || ''
                })(<LwjUpload
                  {...xlsPropsDesign}
                  fileLists={currentContractDetail.designAttachment || ''} />)}
              </FormItem>}
            </Col>
          </Row>
        </Form>
      </Card>}
    </Modal>
  )
}

export default Form.create()(quoteForm);
